package handler;

import java.io.File;
import java.io.FileNotFoundException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import java.util.Scanner;

public class DatabaseConnection {

    private static DatabaseConnection dbInstance;
    private String driverName;
    private Connection conn;

    private String host;
    private String port;
    private String dbNavn;
    private String user;
    private String pass;
    private String db;

    //Checker om forbindelsen til databasen er oprettet uden problemer.
    private boolean hasConnection;

    public static DatabaseConnection getDbInstance() {
        if (dbInstance == null) {
            dbInstance = new DatabaseConnection();
        }
        return dbInstance;
    }

    public void createConnection() {

        //Sætter et String variable på et filnavn. Derefter sættes en ny file til navnet, 
        //og den tekstfil der bliver fundet, scannes igennem for Database login info.
        String filename = "DbLogInfo.txt";
        Scanner textScan;
        try {

            File file = new File(filename);
            textScan = new Scanner(file);
            while (textScan.hasNext()) {
                switch (textScan.next()) {
                    case "Host:":
                        host = textScan.next();
                        break;
                    case "Port:":
                        port = textScan.next();
                        break;
                    case "Dbnavn:":
                        dbNavn = textScan.next();
                        break;
                    case "User:":
                        user = textScan.next();
                        break;
                    case "Password:":
                        if (textScan.hasNext() == false) {
                            pass = "";
                        } else {
                            pass = textScan.next();
                        }
                        break;
                }
            }
            textScan.close();
        } catch (FileNotFoundException ex) {
            System.out.println("db.DBConnection - DBConnection(): kunne ikke finde dbLogInfo." + ex.getMessage());
        }

        db = "jdbc:mysql://" + host + ":" + port + "/" + dbNavn;

        driverName = "com.mysql.jdbc.Driver";

        try {
            
            //Hvis der er forbindelse i forvejen når metoden kaldes, vil den blive lukket.
            if (conn != null) {
                conn.close();
            }

            Class.forName(driverName);

            conn = DriverManager.getConnection(db, user, pass);

            //Hvis programmet når her til uden at støde på exceptions må den have forbindelse.
            hasConnection = true;

            // Håndterer de exceptions som er forbundet med sql i programmerings-
            // sammenhænge
        } catch (ClassNotFoundException ex) {
            System.out.println("Class not found: " + ex.getMessage());
            System.out.println("Husk at tilføje MySQL JDBC Driver under Libraries! (Benyt Add Library...)");
        } catch (SQLException ex) {
            System.out.println("SQL FEJL: " + ex.getMessage());
            hasConnection = false;
        }

    }

    public Connection getConnection() {
        return conn;
    }

    public boolean hasConnection() {
        return hasConnection;
    }
}
